/**
 * 道具管理类
 */
class propMgr extends eui.Component {
	public constructor(gameProps) {
		super();
		this.displayProps = gameProps;
		this.init();
	}
	/**显示道具层 */
	public displayProps: any;

	/**道具容器 */
	public props: prop[] = [];

	/**服务器上的道具 */
	public serverProps: any;

	/**初始化道具数量 */
	private propCountInit = 100;


	private init() {
		this.props = [];
		for (var t = 0; t < this.propCountInit; t++) {
			this.creatOneProps();
		}
	}

	private creatOneProps() {
		var oneProp: prop = new prop(this);
		oneProp.propInit();
		this.props.push(oneProp);
	}

	private getOneProps() {
		for (var t = 0; t < this.props.length; t++) {
			if (this.props[t].isUsed == false)
				return this.props[t];
		}
		/**如果都在使用 创建一个 */
		this.creatOneProps();
		return this.props[this.props.length - 1];
	}


	/**道具更新 */
	public propsUpdata(propsData: any) {
		//console.log("道具容器长度："+this.props.length);
		this.checkServer(propsData);
		this.checkClick(propsData);
		/**道具是否在视野内 不在就不显示 */
		for (let t = 0; t < this.props.length; t++) {
			for (let i = 0; i < propsData.length; i++) {
					 if (this.props[t].x == propsData[i].x && this.props[t].y ==propsData[i].y )
					  {
						  this.props[t].setInRange(propsData[i].isInRange);
					  }
			}
			this.props[t].update();
		}
	}

	/**检查需要添加的 */
	private checkServer(propsData) {
		for (var t = 0; t < propsData.length; t++) {
			var nowProp = propsData[t];
			var propHave = this.clientHaveProps(nowProp);
			if (!propHave) {
				var prop = this.getOneProps();
				if (webNetworkMananger.isServerMove) {
					this.multcreateprop(prop, nowProp);
				}
				else {
					this.singlecreateprop(prop, nowProp);

				}
			}
		}
	}
	/**联网模式生成道具 */
	public multcreateprop(prop, nowProp) {
		//7青蛙死亡生成的荷叶
		if (nowProp.type == 7) {
			prop.leafBronTween(nowProp.point.x, nowProp.point.y, nowProp.id, nowProp.pointfrom.x, nowProp.pointfrom.y, nowProp.value);
		}
		//6地上出现的加分荷叶
		else if (nowProp.type == 6) {
			prop.leafBron(nowProp.point.x, nowProp.point.y, nowProp.id, nowProp.value)
		}
		else {
			prop.propBron("daoju" + nowProp.type, nowProp.point.x, nowProp.point.y, nowProp.id);
		}
	}
	/**单机模式生成道具 */
	public singlecreateprop(prop, nowProp) {
		if (nowProp.type == 7) {
			//7青蛙死亡生成的荷叶
			prop.leafBronTween(nowProp.x, nowProp.y, nowProp.id, nowProp.tweenx, nowProp.tweeny, nowProp.grade);
		}
		//7青蛙死亡生成的荷叶
		else if (nowProp.type == 6) { prop.leafBron(nowProp.x, nowProp.y, nowProp.id, nowProp.grade) }
		else {
			prop.propBron("daoju" + nowProp.type, nowProp.x, nowProp.y, nowProp.id);
		}

	}
	/**检查需要删除的 */
	private checkClick(propsData) {
		var displayProps = this.displayProps.$children;
		displayProps.forEach(e => {
			var needDelet = this.serverHaveProp(e, propsData);
			if (!needDelet)
				e.propRecover();
		})
	}


	/**本地是否有道具 */
	public clientHaveProps(pro) {
		var returnData = false;
		this.props.forEach(e => {
			if (webNetworkMananger.isServerMove) {
				if (pro.type != 7) {
					if (e.x == pro.point.x && e.y == pro.point.y)
					{ returnData = true; }
				}
				if (pro.type == 7) {
					if (e.tweenstart.x == pro.pointfrom.x && e.tweenstart.y == pro.pointfrom.y && e.tweenover.x == pro.point.x && e.tweenover.y == pro.point.y) {
						returnData = true;
					}
				}
			}

			else {
				if (e.x == pro.x && e.y == pro.y)
				{ returnData = true; }
				if (pro.type == 7)
					if (e.tweenstart.x == pro.tweenx && e.tweenstart.y == pro.tweeny && e.tweenover.x == pro.x && e.tweenover.y == pro.y) {
						returnData = true;
					}
			}
		})
		return returnData;
	}


	/**服务器是否有道具 */
	private serverHaveProp(propId, propsData) {
		for (var t = 0; t < propsData.length; t++) {
			//	if (propsData[t].id == propId)
			if (webNetworkMananger.isServerMove) {
				//服务器发送送的数据并不是每次都有pointfrom，避免报空
				if (propsData[t].type != 7) {
					if (propsData[t].point.x == propId.x && propsData[t].point.y == propId.y)
					{ return true; }
				}
				if (propsData[t].type == 7) {
					if (propsData[t].pointfrom.x == propId.tweenstart.x && propsData[t].pointfrom.y == propId.tweenstart.y && propsData[t].point.x == propId.tweenover.x && propsData[t].point.y == propId.tweenover.y)
					{ return true; }
				}
			}
			else {
				if (propsData[t].x == propId.x && propsData[t].y == propId.y)
				{ return true; }
				if (propsData[t].tweenx == propId.tweenstart.x && propsData[t].tweeny == propId.tweenstart.y && propsData[t].x == propId.tweenover.x && propsData[t].y == propId.tweenover.y) {
					return true;
				}

			}
		}
		return false;
	}



}